/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package master.proyectoweb.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import master.proyectoweb.entities.Product;
import master.proyectoweb.managedbeans.util.SearchCriteria;

/**
 *
 * @author joaquin
 */
@Stateless
public class ProductFacade extends AbstractFacade<Product> {
    @PersistenceContext(unitName = "TurnItUpPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ProductFacade() {
        super(Product.class);
    }
    
    //Return list of products according with the search criteria
    public List<Product> searchProducts(int[] range, SearchCriteria searchCriteria) {
        Query query = em.createNamedQuery("Product.search");
        
        if(searchCriteria.isSearchStock()){
            query.setParameter("stock",0);
        }
        else {
            query.setParameter("stock",-1);
        }
        
        if(searchCriteria.getSearchCategory() != null){
            query.setParameter("catname",searchCriteria.getSearchCategory().getName());
        }
        else {
            query.setParameter("catname","%");
        }
        
        if(searchCriteria.getSearchMaxPrice() == 0){
            query.setParameter("maxprice", 1000000);
        }
        else{
            query.setParameter("maxprice", searchCriteria.getSearchMaxPrice());
        }
        
        if(searchCriteria.getSearchTerm() == null){
            query.setParameter("searchterm", "%");
        }
        else {
            query.setParameter("searchterm", "%"+searchCriteria.getSearchTerm()+"%");
        }
        
        query.setParameter("minprice", searchCriteria.getSearchMinPrice());
        
        query.setMaxResults(range[1] - range[0]);
        query.setFirstResult(range[0]);
        return query.getResultList();    
    }

    
    //Return number of products returned by the search
    public int searchCount(SearchCriteria searchCriteria) {
        Query query = em.createNamedQuery("Product.search");
        
        if(searchCriteria.isSearchStock()){
            query.setParameter("stock",0);
        }
        else {
            query.setParameter("stock",-1);
        }
        
        if(searchCriteria.getSearchCategory() != null){
            query.setParameter("catname",searchCriteria.getSearchCategory().getName());
        }
        else {
            query.setParameter("catname","%");
        }
        
        if(searchCriteria.getSearchMaxPrice() == 0){
            query.setParameter("maxprice", 1000000);
        }
        else{
            query.setParameter("maxprice", searchCriteria.getSearchMaxPrice());
        }
        
        if(searchCriteria.getSearchTerm() == null){
            query.setParameter("searchterm", "%");
        }
        else {
            query.setParameter("searchterm", "%"+searchCriteria.getSearchTerm()+"%");
        }
        
        query.setParameter("minprice", searchCriteria.getSearchMinPrice());
        
        return query.getResultList().size();
    }
    
}
